package org.aplombh.java.leetcode.all;

public class _7整数反转 {

}

class Solution7_1 {
    public int reverse(int x) {
        int a = x;

        if (x < 0) {
            a *= -1;
        }

        if (a > 1000000000 && (a % 10 > 2))
            return 0;

        int sum = 0;
        while (a > 0) {
            int r = a % 10;
            sum = r + sum * 10;
            a /= 10;
        }

        if (x < 0) {
            sum *= -1;
        }

        if ((sum ^ x) < 0){
            return 0;
        }

        return sum;
    }
}

class Solution7_2 {
    public int reverse(int x) {
        int sum = 0;
        while (x != 0) {
            if (sum < Integer.MIN_VALUE / 10 || sum > Integer.MAX_VALUE / 10) {
                return 0;
            }
            int r = x % 10;
            x /= 10;
            sum = sum * 10 + r;
        }
        return sum;
    }
}
